//---------------------------------------------------------------------------------------------------------
//
//	UsingKeyDownBrowserController
//
//	Inherits From:		Object
//
//	Declared In:		UsingKeyDownBrowserController.h
//
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//---------------------------------------------------------------------------------------------------------
#import "UsingKeyDownBrowserController.h"
#import "KeyDownBrowser.h"
#import <objc/objc-runtime.h>


@implementation UsingKeyDownBrowserController

//---------------------------------------------------------------------------------------------------------
//	Private Methods
//---------------------------------------------------------------------------------------------------------
- _subclassListFor: (const char*) aClass
{
	id		subclassList = [[List alloc] init];
	Class 	currentClass;
		
	if (classes == NULL) classes = objc_getClasses();

	state = NXInitHashState(classes);

	while ( NXNextHashState(classes, &state, (void **)&currentClass) )
		{
		if (strcmp (object_getClassName (currentClass), aClass) == 0) continue;
		if (strcmp (object_getClassName (currentClass->super_class), aClass) == 0)
			[subclassList addObject: (id)object_getClassName(currentClass)];
		}

	return subclassList;
}


- _subclassListForNil
{
	id		subclassList = [[List alloc] init];
	Class 	currentClass;
		
	if (classes == NULL) classes = objc_getClasses();

	state = NXInitHashState(classes);

	while ( NXNextHashState(classes, &state, (void **)&currentClass) )
		if (currentClass->super_class == NULL) 
			[subclassList addObject: (id)object_getClassName(currentClass)];

	return subclassList;
}


- (BOOL) _isLeaf: (const char*) aClass
{
	id		subclassList = [self _subclassListFor: aClass];
	BOOL	isLeaf;
		
	isLeaf = ([subclassList count] ? NO : YES);
	
	if (subclassList) [subclassList free];
	return isLeaf;
}


static int _Comparator (const void *arg1, const void *arg2)
{
   	return NXOrderStrings (*(char**)arg1, *(char**)arg2, YES, -1, NULL);
}


- _sort: (List*) aList
{
	void*	dataPtr = NX_ADDRESS (aList);
	qsort (dataPtr, [aList count], sizeof (char*), _Comparator);
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Action Methods
//---------------------------------------------------------------------------------------------------------
- singleAction:sender
{
	char		path [256];
	int		selectedColumn = [browser selectedColumn];

	[doubleActionTextField setStringValue: ""];
	if (selectedColumn == -1) 
		{
		[singleActionTextField setStringValue: ""];
		return self;
		}
	[browser getPath:path toColumn: selectedColumn + 1];
	[singleActionTextField setStringValue: path];
	return self;
}


- doubleAction:sender
{
	[doubleActionTextField setStringValue: "Double Action"];
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Application Delegate Methods
//---------------------------------------------------------------------------------------------------------
- appDidInit: sender
{
	[browser setDoubleAction: @selector (doubleAction:)];
	return self;
}


//---------------------------------------------------------------------------------------------------------
//	Browser Delegate Method
//---------------------------------------------------------------------------------------------------------
- (int) browser:aBrowser fillMatrix:aMatrix inColumn:(int)aColumn
{
	id	subclassList;
	int	iterator;
	
	if (aColumn == 0)
		subclassList = [self _subclassListForNil];
	else
		subclassList = [self _subclassListFor: [[browser selectedCell] stringValue]];

	[self _sort: subclassList];

	for (iterator = 0; iterator < [subclassList count]; iterator++)
		{
		[aMatrix renewRows: (iterator + 1) cols:1];
		[[aMatrix cellAt: iterator :0] setStringValue: (char*)[subclassList objectAt: iterator]];
		[[aMatrix cellAt: iterator :0] setLeaf: [self _isLeaf: (char*)[subclassList objectAt: iterator]]];
		}
		
	if (subclassList) [subclassList free];
	return iterator;
}
	
	
@end